﻿@model ReportByUserListModel
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = "维修设备统计";
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("ReportEquipment list");
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="content-header clearfix">
        <h1 class="pull-left">
            维修设备统计
        </h1>
        <div class="pull-right">
            <button class="btn btn-success" type="submit" name="exportbyequipmentexcel-all">
                <i class="fa fa-file-excel-o"></i>
                @T("Admin.Common.ExportToExcel.All")
            </button>
        </div>
    </div>

    <div class="content">
        <div class="form-horizontal">
            <div class="panel-group">
                <div class="panel panel-default panel-search">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-2 col-search">
                                @Html.NopLabelFor(model => model.SearchMonth)
                                <div class="col-content">
                                    @Html.NopEditorFor(model => model.SearchMonth)
                                </div>
                            </div>
                            <div class="col-md-2 col-search">
                                @Html.NopLabelFor(model => model.SearchDepartId)
                                <div class="col-content">
                                    @Html.EditorFor(model => model.SearchDepartId, new { SelectList = Model.AvailableDepartmentMenus })
                                </div>
                            </div>
                            <div class="col-md-2 col-search">
                                @Html.NopLabelFor(model => model.SearchUserId)
                                <div class="col-content">
                                    @Html.NopDropDownListFor(model => model.SearchUserId, Model.AvailableUsers)
                                </div>
                            </div>
                            <div class="col-md-2">
                                <button type="button" id="search-customers" class="btn btn-primary"><i class="fa fa-search"></i>@T("Admin.Common.Search")</button>
                                <button type="button" id="cancel-customers" class="btn btn-default"><i class="fa fa-refresh"></i>重置</button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div id="customers-grid"></div>

                        <script>
                        $(document).ready(function() {
                            $("#customers-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("ReportByEquipments", "Maintenance"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: additionalData
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors"
                                    },
                                    requestEnd: function (e) {
                                        if (e.type == "read") {
                                            var response = e.response;
                                            if (response) {
                                                //store extra data
                                                reportE = e.response["ExtraData"];
                                            }
                                        }
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    pageSize: @(defaultGridPageSize),
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    pageSizes: [@(gridPageSizes)],
                                    @Html.Partial("_GridPagerMessages")
                                },
                                editable: {
                                    confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                    mode: "inline"
                                },
                                scrollable: false,
                                dataBound: onDataBoundE,
                                columns: [{
                                            field: "Name",
                                    title: "名称",
                                    width: 100,
                                            headerAttributes: { style: "text-align:center" },
                                        },{
                                            field: "Manufacturer",
                                    title: "品牌",
                                    width: 100,
                                            headerAttributes: { style: "text-align:center" },
                                        },{
                                            field: "Specification",
                                    title: "规格型号",
                                    width: 100,
                                            headerAttributes: { style: "text-align:center" },
                                    }, {
                                        field: "HoursString",
                                        title: "共计工时",
                                        width: 100,
                                        headerAttributes: { style: "text-align:center" },
                                        attributes: { style: "text-align:right" },
                                        footerTemplate: '<div style="text-align:right;"><span id=\"e-total-hour\"></span></div>'
                                    }, {
                                            field: "Amount",
                                    title: "共计次数",
                                    width: 100,
                                            headerAttributes: { style: "text-align:center" },
                                            attributes: { style: "text-align:center" },
                                            footerTemplate: '<div style="text-align:center;"><span id=\"e-total-amount\"></span></div>'
                                        },{
                                            field: "Per",
                                    title: "平均工时",
                                    width: 100,
                                            headerAttributes: { style: "text-align:center" },
                                            attributes: { style: "text-align:right" },
                                        }]
                            });
                            //search button
                            $('#search-customers').click(function () {
                                var grid = $('#customers-grid').data('kendoGrid');
                                grid.dataSource.page(1); //new search. Set page size to 1
                                //grid.dataSource.read(); we already loaded the grid above using "page" function
                                selectedIds = [];
                                return false;
                            });
                            //cancel button
                            $('#cancel-customers').click(function () {
                                //cancel
                                var grid = $('#customers-grid').data('kendoGrid');
                                $('#@Html.FieldIdFor(model => model.SearchMonth)').val('');
                                $('#@Html.FieldIdFor(model => model.SearchDepartId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchUserId)').val('0');
                                $("#select_menuSearchDepartId").attr("value", "全部");
                                grid.dataSource.page(1); //new search. Set page size to 1
                                //grid.dataSource.read(); we already loaded the grid above using "page" function
                                return false;
                            });
                            $("#@Html.FieldIdFor(model => model.SearchDepartId)").change(function () {
                                var selectedItem = $(this).val();
                                var ddlStates = $("#@Html.FieldIdFor(model => model.SearchUserId)");
                                $.ajax({
                                    cache: false,
                                    type: "GET",
                                    url: "@(Url.Action("GetUsersByDepartmentId", "Customer"))",
                                    data: { "departId": selectedItem, onlyRepairman: true },
                                    success: function(data) {
                                        ddlStates.html('');
                                        $.each(data, function(id, option) {
                                            ddlStates.append($('<option></option>').val(option.id).html(option.name));
                                        });
                                    },
                                    error: function(xhr, ajaxOptions, thrownError) {
                                        alert('Failed to retrieve states.');
                                    }
                                });
                            });
                        });
                        </script>
                        <script type="text/javascript">

                            function additionalData() {
                                var data = {
                                SearchMonth: $('#@Html.FieldIdFor(model => model.SearchMonth)').val(),
                                SearchDepartId: $('#@Html.FieldIdFor(model => model.SearchDepartId)').val(),
                                SearchUserId: $('#@Html.FieldIdFor(model => model.SearchUserId)').val(),
                                };
                                addAntiForgeryToken(data);
                                return data;
                            }

                            function onDataBoundE(e) {
                                //update order totals summary
                                if (reportE != "") {
                                    $('#e-total-amount').text(reportE['Amount']);
                                    $('#e-total-hour').text(reportE['HoursString']);
                                }
                            }
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
}
